package com.xiaofei.net;

import java.net.URI;
import java.util.Locale;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.xiaofei.model.NetModel;

public class HttpGetCon implements INetConnection{

	URI uri;
	NetModel mModel;

	public HttpGetCon(URI uri) {
		this.uri = uri;
		mModel = new NetModel();
	}

	@Override
	public NetModel getNetInfo() {
		// TODO Auto-generated method stub
		HttpClient getClient = new DefaultHttpClient();
		HttpGet request = new HttpGet(uri);
		HttpResponse response;
		try {
			  long time1 = System.currentTimeMillis();
			response = getClient.execute(request);
			 long time2 = System.currentTimeMillis();
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				
			    mModel.setmResponseCode(response.getStatusLine().getStatusCode());
			    mModel.setmLinkTime(time2-time1);
			    if(response.getEntity()!=null){
			    	mModel.setmContentLength(response.getEntity().getContentLength());
			    	if(response.getEntity().getContentEncoding()!=null){
			    		Header header = response.getEntity().getContentEncoding();
			    		mModel.setmEnCodingHeaderName(header.getName());
			    		mModel.setmEnCodingHeaderValue(header.getValue());
			    	}
			    	if(response.getEntity().getContentType()!=null){
			    		Header header = response.getEntity().getContentType();
			    		mModel.setmTypeHeaderName(header.getName());
			    		mModel.setmTypeHeaderValue(header.getValue());
			    	}
			    	mModel.setChunk(response.getEntity().isChunked());
			    	mModel.setRepeat(response.getEntity().isRepeatable());
			    }
			    if(response.getLocale()!=null){
			    	Locale locale = response.getLocale();
			    	mModel.setmLanguage(locale.getLanguage());
			    	mModel.setmCountry(locale.getCountry());
			    	mModel.setmDisplayLanguage(locale.getDisplayLanguage());
			    	mModel.setmDisplayCountry(locale.getDisplayCountry());
			    }
				
			}
		
		}  catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return mModel;
	}
}